@include('Api.common.header')
<div class="layui-row">
    <div class="layui-col-lg12">
        <fieldset class="layui-elem-field">
            <legend>权限管理 - 分配权限</legend>
            <div class="layui-field-box">
                <form class="layui-form" action="{{ URL::action('Api\RoleController@setPermission')}}">
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色名称</label>
                        <div class="layui-input-inline">
                            <input type="text" placeholder="请输入角色名称" class="layui-input"
                                   value="{{$roleRes['title']}}">
                            <input type="hidden" name="id" value="{{$roleRes['id']}}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">拥有权限</label>
                        @foreach($ruleRes as $rule)
                        <div class="layui-input-block">
                            <input type="checkbox" name="rule_id[]" title="{{$rule['title']}}" value="{{$rule['id']}}" lay-filter="rule_id" lay-skin="primary"
                                   data-parent-id="{{$rule['parent_id']}}" @if(in_array($rule['id'],$currentRules) ) checked @endif>
                            @if(key_exists('child',$rule))
                                @foreach($rule['child'] as $val)

                            <div class="layui-input-block" style="margin-left: 46px;">
                                <input type="checkbox" name="rule_id[]" title="{{$val['title']}}" value="{{$val['id']}}" lay-filter="rule_id" lay-skin="primary"
                                       data-parent-id="{{$val['parent_id']}}" @if(in_array($val['id'],$currentRules)) checked @endif>
                                @if(key_exists('child',$val))
                                <div class="layui-input-block" style="margin-left: 46px;">
                                    @foreach($val['child'] as $vo)
                                    <div class="layui-input-inline" style="width: auto;">
                                        <input type="checkbox" name="rule_id[]" title="{{$vo['title']}}" value="{{$vo['id']}}" lay-skin="primary"
                                               lay-filter="rule_id"
                                               data-parent-id="{{$vo['parent_id']}}" @if(in_array($vo['id'],$currentRules)) checked @endif>
                                    </div>
                                    @endforeach
                                    <div style="clear: both"></div>
                                </div>
                                @endif

                            </div>
                                @endforeach
                            @endif

                        </div>
                        @endforeach
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                        </div>
                    </div>
                </form>

            </div>
        </fieldset>
    </div>
</div>
<script type="text/javascript" src="/static/layui/layui.js"></script>
<script type="text/javascript" src="/static/backend/javascript/dw_form_submit.js"></script>
<script type="text/javascript">
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer;
        var form  = layui.form;

        $("body div").removeClass("layui-container");
        /*分配权限页面-选择联动*/
        form.on('checkbox(rule_id)', function (data) {
            var currentId = data.value;
            var parentId  = data.elem.dataset.parentId;
            var isChecked = data.elem.checked;
            if (isChecked) {
                checkAllParent(parentId);
                checkAllChild(currentId);
            } else {
                cancelAllChild(currentId);
                cancelAllParent(parentId);
            }
            form.render(); //更新全部
        });
        /*递归选择上级*/
        function checkAllParent(parentId) {
            if (parentId == 0) return false;
            if (parentId == undefined) return false;
            var parent = $("input[value=" + parentId + "]");
            parent.prop("checked", true);
            var fatherId = parent.attr("data-parent-id");
            checkAllParent(fatherId);
        }

        /*递归选择下级*/
        function checkAllChild(id) {
            var son = $("input[data-parent-id=" + id + "]");
            $.each(son, function () {
                $(this).prop("checked", true);
                var sonId = $(this).val();
                checkAllChild(sonId);
            });
        }

        /*递归取消下级*/
        function cancelAllChild(id) {
            var son = $("input[data-parent-id=" + id + "]");
            $.each(son, function () {
                $(this).prop("checked", false);
                var sonId = $(this).val();
                cancelAllChild(sonId);
            });
        }

        /*递归取消下级*/
        function cancelAllParent(parentId) {
            if (parentId == 0) return false;
            if (parentId == undefined) return false;
            //需要判断同级是否有被选中的，如果有那么不能取消上级
            var flag    = true;//默认取消
            var borther = $("input[data-parent-id=" + parentId + "]");
            $.each(borther, function () {
                var hasChecked = $(this).prop("checked");
                if (hasChecked) {
                    flag = false;//同级存在被选中的，不能取消上级
                    return false;
                }
            });
            if (flag) {
                var parent = $("input[value=" + parentId + "]");
                parent.prop("checked", false);
                var fatherId = parent.attr("data-parent-id");
                cancelAllParent(fatherId);
            }
        }

    });
</script>
